package fun.abbas.myapplication;

import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.zx.sercurity.AsyncResultWrapper;
import com.zx.sercurity.RiskControlManager;
import com.zx.sercurity.RiskEnum;
import com.zx.sercurity.RiskListener;
import com.zx.sercurity.RiskResult;
import com.zx.sercurity.AsyncRiskListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

import fun.abbas.myapplication.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity implements RiskListener, AsyncRiskListener {

    private static final String TAG = "MainActivity";
    private final MyAdapter adapter = new MyAdapter();
    private final MyAdapter2 adapter2 = new MyAdapter2();

    private fun.abbas.myapplication.databinding.ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(binding.appbar, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        binding.recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.recycler.setAdapter(adapter);
        binding.syncResult.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        binding.syncResult.setAdapter(adapter2);

        RiskControlManager.getInstance().addRiskCheckListener(this);
        RiskControlManager.getInstance().addAsyncRiskCheckListener(this);
        RiskControlManager.getInstance().startRiskCheck(this);
//        RiskResult<?> riskResult = RiskControlManager.getInstance().startRiskCheck(this);
//        RiskEnum type = riskResult.getType();
//        Object data = riskResult.getData();

        RiskControlManager.getInstance().startAsyncRiskCheck(this);
//
//        RiskResult.RootRiskResult rootRiskResult = new RootRisk().riskCheck(this);
//        RiskResult.EmulatorRiskResult emulatorRiskResult = new EmulatorRisk().riskCheck(this);
//        RiskResult.NoSIMRiskResult noSIMRiskResult = new SimRisk().riskCheck(this);
//        RiskResult.DebugModeRiskResult debugModeRiskResult = new DebugModeRisk().riskCheck(this);
//        RiskResult.NetProxyRiskResult netProxyRiskResult = new NetProxyRisk().riskCheck(this);
//        RiskResult.VPNRiskResult vpnRiskResult = new VPNRisk().riskCheck(this);
//        RiskResult.MTManagerRiskResult mtManagerRiskResult = new MTRisk().riskCheck(this);
//        RiskResult.CustomCertificatesRiskResult customCertificatesRiskResult = new CustomCertificatesRisk().riskCheck(this);
//        RiskResult.AccessibilityServiceRiskResult accessibilityServiceRiskResult = new AccessibilityServiceRisk().riskCheck(this);
//
//        MovementDetectorRisk movementDetectorRisk = new MovementDetectorRisk(new RiskConsumer<RiskEnum>() {
//            @Override
//            public void accept(RiskEnum var1) {
//
//            }
//        });
//        BatteryRisk batteryRisk = new BatteryRisk(new RiskConsumer<RiskEnum>() {
//            @Override
//            public void accept(RiskEnum var1) {
//
//            }
//        });
//        batteryRisk.beginCheck(this);
//        movementDetectorRisk.beginCheck(this);
//        ChargingRisk chargingRisk = new ChargingRisk(new RiskConsumer<RiskEnum>() {
//            @Override
//            public void accept(RiskEnum var1) {
//
//            }
//        });
//        chargingRisk.beginCheck(this);
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                Log.e("lmptl", "Stop All Task");
//                RiskControlManager.getInstance().stopAllRunTask();
//            }
//        },3000);

//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                Log.e("lmptl", "start All Task Again");
//                RiskControlManager.getInstance().startSyncRiskCheck(MainActivity.this);
//            }
//        },8000);

//        binding.testLifecycleStopThread.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                startActivity(new Intent(
//                        MainActivity.this,
//                        LifecycleTestActivity.class
//                ));
//            }
//        });
    }


    @Override
    public void onRisk(ArrayList<RiskResult<?>> risks) {
        adapter2.submitList(risks);
    }

    private final HashMap<RiskEnum, AsyncResultWrapper<?>> dataMap = new HashMap<>();

    @Override
    public void onAsyncRiskStateChange(AsyncResultWrapper<?> asyncResultWrapper) {
        dataMap.put(asyncResultWrapper.getRiskEnum(), asyncResultWrapper);
        Log.i("onAsyncRiskStateChange", asyncResultWrapper.getRiskEnum().name() + " " + asyncResultWrapper.getState().name());
        adapter.submitList(new ArrayList<>(dataMap.values()));
    }

    @Override
    public void onFindAsyncRisk(AsyncResultWrapper<?> asyncResultWrapper) {
        dataMap.put(asyncResultWrapper.getRiskEnum(), asyncResultWrapper);
        Log.i("onFindAsyncRisk", asyncResultWrapper.getRiskEnum().name() + " " + asyncResultWrapper.getState().name());
        adapter.submitList(new ArrayList<>(dataMap.values()));
    }
}